package com.example.actordemo1.routering;

import akka.actor.*;
import akka.japi.pf.DeciderBuilder;
import akka.routing.Broadcast;
import akka.routing.FromConfig;
import akka.routing.RoundRobinGroup;

import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;

/**
 * 组路由actor
 */
public class GroupRouterActor extends AbstractActor {



    ActorRef router = null;

    @Override
    public void preStart() throws Exception {

        getContext().actorOf(Props.create(RouterActor.class),"taskActor1");
        getContext().actorOf(Props.create(RouterActor.class),"taskActor2");
        getContext().actorOf(Props.create(RouterActor.class),"taskActor3");


        List<String> routeePath = Arrays.asList( "/user/groupRouterActor/taskActor1",
        "/user/groupRouterActor/taskActor2",
        "/user/groupRouterActor/taskActor3");
        //直接创建
        router = getContext().actorOf(new RoundRobinGroup(routeePath)
                .props(),"router111");
        //配置 deployment 创建
//        router =  getContext().actorOf(FromConfig.getInstance().props(),"router");

        System.out.println("router:"+router);
    }



    @Override
    public Receive createReceive() {
        return receiveBuilder().matchAny(o->{
            router.tell(o,getSender());
        }).build();
    }

    public static void main(String[] args) {
        ActorSystem actorSystem = ActorSystem.create("sys");
        ActorRef actorRef = actorSystem.actorOf(Props.create(GroupRouterActor.class), "groupRouterActor");
        actorRef.tell("helloA",ActorRef.noSender());
        actorRef.tell("hellob",ActorRef.noSender());
        actorRef.tell("helloc",ActorRef.noSender());

        //广播
//        actorRef.tell(new Broadcast("atch out for Davy Jones"),ActorRef.noSender());

    }
}
